<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/1/11 00:12
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Delete_Model extends API_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param $params
     */
    public function run($params)
    {
        $send['state'] = false;
        do {

            if (empty($params['orderId']) || empty($params['userId']) || empty($params['sn'])) {
                $send['error'] = '参数错误';
                break;
            }

            $orderDb = clone($this->db);
            $q = $orderDb->where(['orderId' => $params['orderId']])->get();
            $row = $q->row_array();
            if (empty($row)) {
                $send['error'] = '订单不存在';
                break;
            }

            if ($row['userId'] != $params['userId'] || $row['sn'] != $params['sn']) {
                $send['error'] = '该用户无此订单';
                break;
            }

            if ($row['status'] != 'Unpaid' || $row['status'] != 'Cancel') {
                $send['error'] = '订单状态不可更改';
                break;
            }

            $db = clone($this->db);
            $db->where(['id' => $params['orderId']]);
            $db->delete('user_order');

            if ($db->affected_rows()) {
                $send['state'] = true;
                $send['data'] = [
                    'msg' => '订单删除成功'
                ];
            } else {
                $send['error'] = '订单删除失败';
            }

        } while(false);
        $this->output->send($send);
    }
}

